package tech.yunland.shop.domain.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.moyuer.bsf.domain.model.AbstractVersionEntity;
import com.moyuer.bsf.domain.model.Organ;
import com.moyuer.bsf.domain.model.User;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.Date;

/**
 * 预购商品汇总
 * <p>
 * <p>
 * 充值后, 应将商品汇总入本对象
 * </p>
 * <p>
 * 收银后, 应从本对象中减去相应数量的商品
 * </p>
 * 
 * @author: 袁首京<yuanshoujing@gmail.com> 16/4/22
 */
@Entity
@Table(name = "goods_paid")
public class GoodsPaid extends AbstractVersionEntity {

	@JSONField(serialize = false)
	@ManyToOne
	private User member; // 会员

	@ManyToOne
	private Goods goods; // 商品

	@NotNull
	private BigDecimal quantity = BigDecimal.ZERO; // 数量

	@NotNull
	private BigDecimal amount = BigDecimal.ZERO; // 金额

	@JSONField(serialize = false)
	@ManyToOne
	private Organ organ; // 所属机构

	@JSONField(format = "yyyy-MM-dd HH:mm:ss")
	@Column(name = "last_modify", nullable = false)
	private Date lastModify = new Date(); // 更新时间

	@Transient
	private BigDecimal realPrice; //真实价值

	@Transient
	private String memberId;
	@Transient
	private String goodsName;
	@Transient
	private String memberName;
	@Transient
	private BigDecimal memberPrize=BigDecimal.ZERO;// 商品会员价格

	public BigDecimal getMemberPrize() {
		
		if (quantity.compareTo(BigDecimal.ZERO)==0) {
			
			return goods.getPrice();
		}
			return amount.divide(quantity, 2, BigDecimal.ROUND_HALF_UP);
	}

	public void setMemberPrize(BigDecimal memberPrize) {
		this.memberPrize = memberPrize;
	}

	public User getMember() {

		return member;
	}

	public void setMember(User member) {
		this.member = member;
	}

	public Goods getGoods() {
		return goods;
	}

	public void setGoods(Goods goods) {
		this.goods = goods;
	}

	public BigDecimal getQuantity() {
		return (quantity == null ? BigDecimal.ZERO : quantity);
	}

	public void setQuantity(BigDecimal quantity) {
		this.quantity = quantity;
	}

	public BigDecimal getAmount() {
		return (amount == null ? BigDecimal.ZERO : amount);
	}

	public void setAmount(BigDecimal amount) {
		this.amount = amount;
	}

	public Organ getOrgan() {
		return organ;
	}

	public void setOrgan(Organ organ) {
		this.organ = organ;
	}

	public Date getLastModify() {
		return lastModify;
	}

	public void setLastModify(Date lastModify) {
		this.lastModify = lastModify;
	}

	public String getMemberId() {
		if (memberId != null) {
			return memberId;
		}

		if (member != null) {
			memberId = member.getId();
		}

		return memberId;
	}

	public void setMemberId(String memberId) {
		this.memberId = memberId;
	}

	public String getGoodsName() {
		if(goodsName!= null){
			return goodsName;
		}
		if(goods!=null){
			goodsName=goods.getName();
		}
		return goodsName;
	}

	public void setGoodsName(String goodsName) {
		this.goodsName = goodsName;
	}

	public String getMemberName() {
		if (memberName != null) {
			return memberName;
		}

		if (member != null) {
			memberName = member.getName();
		}

		return memberName;
	}

	public void setMemberName(String memberName) {
		this.memberName = memberName;
	}

	public BigDecimal getRealPrice() {
		if (this.goods != null){
			return this.quantity.multiply(this.goods.getPrice());
		}
		return BigDecimal.ZERO;
	}

	public void setRealPrice(BigDecimal realPrice) {
		this.realPrice = realPrice;
	}
}
